<?php

namespace loggedin\controller;

require_once("/loggedin/view/QuestionView.php");
require_once("/loggedin/model/GetQuestions.php");
require_once("/login/view/Userinfo.php");
require_once("/loggedin/model/AddQuestion.php");
require_once("/loggedin/model/AlterQuestionModel.php");

class AlterQuestion {
	/**
	 * @var \loggedin\view\QuestionView
	 */
	private $questionView;
	/**
	 * @var \login\view\UserInfo
	 */
	private $userInfo;
	/**
	 * @var \loggedin\model\GetQuestions
	 */
	private $getQuestions;
	/**
	 * @var \loggedin\model\AddQuestion
	 */
	private $addQuestion;
	/**
	 * @var \loggedin\model\AlterQuestionModel
	 */
	private $alterQuestionModel;

	function __construct(\loggedin\view\QuestionView $questionView) {
		$this->questionView = $questionView;
		$this->userInfo = new \login\view\UserInfo();
		$this->getQuestions = new \loggedin\model\GetQuestions();
		$this->addQuestion = new \loggedin\model\AddQuestion();
		$this->alterQuestionModel = new \loggedin\model\AlterQuestionModel();
	}

	/**
	 * @return HTML string
	 */
	public function alterQuestionPage() {
		$questionID = $this->questionView->getAlterQuestionIDValue();
		$this->questionView->setAlterQuestionIDCookie($questionID);
		$username = $this->userInfo->getUserLoggedinInfo();
		$mysqli = new \mysqli("mysql08.citynetwork.se", "119958-mg18906",
							  "quizzer50", "119958-quizzer");
		$question = $this->getQuestions->getQuestion($questionID, $mysqli, $username);
		return $this->questionView->getAlterQuestionPage($question);
	}
	
	public function changeQuestion() {
		$question = $this->questionView->getNewQuestionInfo();
		$mysqli = new \mysqli("mysql08.citynetwork.se", "119958-mg18906",
						  	  "quizzer50", "119958-quizzer");
		try {
			$this->addQuestion->validateNewQuestion($question);
			$questionID = $this->questionView->getAlterQuestionIDCookie();
			$this->alterQuestionModel->changeQuestion($question, $questionID, $mysqli);
			$this->questionView->setChangeQuestionSuccessMessage();
		}
		catch (\Exception $e) {
			$this->questionView->setErrorMessage($e->getMessage());
		}
		$mysqli->close();
	}
}